package se.teamscape.client.presenter;

import net.customware.gwt.presenter.client.EventBus;
import se.teamscape.client.event.RpcDoneEvent;
import se.teamscape.client.event.RpcDoneEventHandler;
import se.teamscape.client.event.RpcStartedEvent;
import se.teamscape.client.event.RpcStartedEventHandler;

import com.google.inject.Inject;

/**
 * Top level presenter. Acts as a container for other container presenters. The
 * mapped view is our top level layout structure.
 * 
 * @author Andreas Borglin
 * 
 */
public class AppPresenter extends BaseContainerPresenter<AppPresenter.Display> implements RpcStartedEventHandler,
        RpcDoneEventHandler {

    public interface Display extends BaseContainerPresenter.Display {
        void showLoadingMsg();

        void hideLoadingMsg();
    }

    @Inject
    public AppPresenter(Display display, EventBus eventBus, NorthContainerPresenter north, SouthContainerPresenter south,
            WestContainerPresenter west, EastContainerPresenter east, CenterContainerPresenter center) {
        // center must be added last since no widgets can be added after
        // the center widget in DockLayoutPanel
        super(display, eventBus, north, south, west, east, center);

        eventBus.addHandler(RpcStartedEvent.TYPE, this);
        eventBus.addHandler(RpcDoneEvent.TYPE, this);
    }

    /*
     * Explicitly reveal all child presenters.
     */
    public void revealAll() {
        bind();
        revealDisplay();
        showAllPresenters();
    }

    /*
     * Override to make sure that we don't listen to PresenterRevealedEvents.
     * Since all our presenters are active simultaneously and constantly,
     * it makes no sense to bother with presenter revelead here.
     * (non-Javadoc)
     * 
     * @see se.teamscape.client.presenter.BaseContainerPresenter#onBind()
     */
    @Override
    protected void onBind() {
        onBind(false);
    }

    @Override
    public void onRpcStarted() {
        display.showLoadingMsg();
    }

    @Override
    public void onRpcDone() {
        display.hideLoadingMsg();
    }
}
